function ContextMenu (idContextMenu, menuText, menuIds) {
  this.idContextMenu = $(idContextMenu);
  this.idContextMenu.hide();
  this.menuText = menuText;
  this.menuIds = menuIds;
  this.idEntity = 1;
  this.idSentiment = 1;
  this.word = "";
  this.displayMenu();
  this.hideContext();
  this.wordClicked();
}

ContextMenu.prototype.displayMenu = function(){
  var self = this;
  $("#textarea").bind("contextmenu", function(e){
    e.preventDefault();
    self.idContextMenu.css({
      top: e.pageY + "px",
      left: e.pageX + 10 + "px"
    })
    self.idContextMenu.hide().show(500);

  })
};

ContextMenu.prototype.hideContext = function() {
  var self = this;
  $(document).bind("click", function(e) {
    if(e.pageX < self.idContextMenu.position().left ||
      e.pageX > self.idContextMenu.position().left + self.idContextMenu.width() ||
      e.pageY < self.idContextMenu.position().top ||
      e.pageY > self.idContextMenu.position().top + self.idContextMenu.height()) {
      self.idContextMenu.hide(200);
    }
  })
};

ContextMenu.prototype.wordClicked = function() {
  var self = this;
  var span;
  $("span").bind("click contextmenu", function (e) {
    span = this;
    if(/<entity/i.test($(span).html())) 
    {
      $("#entity, #sentimentGroup").hide();
      $("#remove").show();
    }
    else if(/<sentimentgroup/i.test($(span).html())) 
    {
      $("#entity, #sentimentGroup").hide();
      $("#remove").show();
    }
    else 
    {
      $("#entity, #sentimentgroup").show();
      $("#remove").hide();
    }
  });

  $("#entity").bind("click", function(e) {
    self.idContextMenu.hide();
    
    $("#dialog-message-entitate").attr("title", "Proprietati entitate").html(
      "<p>ID: " + self.idEntity + "</p>" +
      "<p>Valoare sentiment pentru entitate: " +
      "<select>" + 
      "<option value=\"-4\">-4</option>"+
      "<option value=\"-3\">-3</option>" +
      "<option value=\"-2\">-2</option>" +
      "<option value=\"-1\">-1</option>" +
      "<option value=\"0\" selected>0</option>" +
      "<option value=\"1\">1</option>" +
      "<option value=\"2\">2</option>" +
      "<option value=\"3\">3</option>" +
      "<option value=\"4\">4</option>" +
      "</select> " +
      "</p>" );
    $( "#dialog-message-entitate" ).dialog( "destroy" );
    $( "#dialog-message-entitate").dialog({
      modal: true,
      buttons: {
        Ok: function() {
          $("#dialog-message-entitate").attr("title", "").html("");
          
          $(span).html("<entity id = \"" + self.idEntity + "\">" + $(span).html() + "</entity>");
          self.idEntity++;
          $( this ).dialog( "close" );
        }
      }
    });
  });

  $("#sentimentGroup").bind("click", function(e) {
    self.idContextMenu.hide();
    
    $("#dialog-message-sentiment").attr("title", "Proprietati SentimentGroup").html(
      "<p>ID: " + self.idSentiment + "</p>" +
      "<p>Sentiment Value: " +
      "<select>" + 
      "<option value=\"-4\">-4</option>"+
      "<option value=\"-3\">-3</option>" +
      "<option value=\"-2\">-2</option>" +
      "<option value=\"-1\">-1</option>" +
      "<option value=\"0\" selected>0</option>" +
      "<option value=\"1\">1</option>" +
      "<option value=\"2\">2</option>" +
      "<option value=\"3\">3</option>" +
      "<option value=\"4\">4</option>" +
      "</select> " +
      "</p>" );
    $( "#dialog-message-sentiment" ).dialog( "destroy" );
    $( "#dialog-message-sentiment").dialog({
      modal: true,
      buttons: {
        Ok: function() {
          $("#dialog-message-sentiment").attr("title", "").html("");
          
          $(span).html("<sentimentgroup id = \"" + self.idSentiment + "\">" + $(span).html() + "</sentimentgroup>");
          self.idSentiment++;
          $( this ).dialog( "close" );
        }
      }
    });
  });

  $("#remove").bind("click", function(e) {
    self.idContextMenu.hide();
    var clickedId;
    // ENTITY
    if($($(span.childNodes)).get(0).nodeName.toLowerCase() == "entity") {
      clickedId = $($(span.childNodes)).attr("id");
      self.idEntity--;
      $("entity").each(function(){
        if($(this).attr("id") > clickedId)
          $(this).attr("id", ($(this).attr("id") - 1));
      });
    }
    // SENTIMENTGROUP
    if($($(span.childNodes)).get(0).nodeName.toLowerCase() == "sentimentgroup") {
      clickedId = $($(span.childNodes)).attr("id");
      self.idSentiment--;
      $("sentimentGroup").each(function(){
        if($(this).attr("id") > clickedId)
          $(this).attr("id", ($(this).attr("id") - 1));
      });
    }
      $(span.childNodes).contents().unwrap();
  });

  span = null;
}